<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Models_Mysql_Widget extends Modules_Core_Models_Widget
{
	public function getById($id, $moduleId = null)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'core_widget
							WHERE widget_id = "' . $this->_conn->escape($id) . '"';
		if ($moduleId) {
			$sql .= ' AND module_id = "' . $this->_conn->escape($moduleId) . '"';
		}
		$sql .= ' LIMIT 1';
		$rs = $this->_conn->query($sql);
		$return = (0 == mysql_num_rows($rs)) ? null : $this->_conn->fetchAll($rs);
		return $return;
	}
	
	public function dbWidgets($module, $all = false)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'core_widget 
					WHERE module_id = "' . $this->_conn->escape($module) . '"';
		if ($all == false) {
			$sql .= ' AND SUBSTRING(widget_id, -7) != "toolbox"';
		}
		$sql .= ' ORDER BY module_id, widget_id ASC';
		$rs = $this->_conn->query($sql);
		$widgets = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$widgets[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $widgets;
	}
	
	public function dbWidgetsToolbox($module = null)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'core_widget 
					WHERE widget_id LIKE "%toolbox"';
		if ($module) {
			 $sql .= ' AND module_id = "' . $this->_conn->escape($module) . '"';
		}
		$sql .= ' ORDER BY module_id, widget_id ASC';
		$rs = $this->_conn->query($sql);
		$widgets = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$widgets[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $widgets;
	}
	
	public function add($widget)
	{
		$this->_conn->insert($widget, $this->_conn->_tablePrefix . 'core_widget');
	}
	
	public function find($condition = array(), $offset = null, $limit = null)
	{
		$sql = 'SELECT * FROM ' . $this->_conn->_tablePrefix . 'core_user AS u ORDER BY u.username ASC';
		
		if ($offset !== null && $limit !== null) {
			$sql .= ' LIMIT ' . (int)$offset . ',' . (int)$limit;
		}
		$rs = $this->_conn->query($sql);
		$users = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$users[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $users;
	}
	
	public function count($condition = array())
	{
		$sql = 'SELECT COUNT(*) as num_users FROM ' . $this->_conn->_tablePrefix . 'core_user';
		$where = ' WHERE 1';
		if (isset($condition['role_id'])) {
			$where .= ' AND role_id = "' . $this->_conn->escape($condition['role_id']) . '"';
		}
		$sql .= $where;
		$rs = $this->_conn->query($sql);
		$row = $this->_conn->fetchAll($rs);
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $row['num_users'];
	}
	
	public function delete($id, $moduleId = null)
	{
		$sql = 'DELETE FROM ' . $this->_conn->_tablePrefix . 'core_widget 
							WHERE widget_id = "' . $this->_conn->escape($id) . '"';
		if ($moduleId) {
			$sql .= ' AND module_id = "' . $this->_conn->escape($moduleId) . '"';
		}
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
}